<?php


namespace app\admin\controller;


use app\common\lib\Auth;
use think\Db;
use think\Request;
use app\common\lib;

class Home extends Base
{
    public function home_index()
    {
        if(request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //关键字查询
            if(isset($keyword) && $keyword!="")
            {
                $map['username'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($type_name) && $type_name!="")
            {
                $map['type_name'] = $type_name;
            }
            if(isset($home_name) && $home_name!="")
            {
                $map['home_name'] = $home_name;
            }
            if(isset($index_name) && $index_name!="")
            {
                $map['index_name'] = $index_name;
            }
            if(isset($status) && $status!="")
            {
                $map['status'] = $status;
            }
            try {
                $count = Db::name('home')->where($map)->page($NowPage,$limits)->count();  //总条目数据
                $lists = Db::name('home')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $_type  = Model('MemberType')->get_status_all_msg('1');   //楼栋
        $_index = Model('MemberIndex')->get_status_all_msg('1');   //单元门
        $_home  = Model('MemberHome')->get_status_all_msg('1');   //房间号
        $this->assign([
            '_type' => $_type,
            '_index' => $_index,
            '_home' => $_home,
        ]);
        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        return $this->fetch();
    }

    public function home_index_add()
    {
        if(\request()->isAjax()){
            $data = input('post.');
            //检测两次密码是否一致
            $is_pwd = lib\Password::isPassword($data['password'],$data['repassword']);
            if(!$is_pwd) {
                return show('200','两次密码请保持一致!','0');
            }
            //检测是否该用户已注册
            $_is_has = Model('Home')->get(['type_name'=>$data['type_name'],'index_name'=>$data['index_name'],'home_name'=>$data['home_name']]);
            if($_is_has) {
                return show('200','该业主已经被注册!如有疑问可以具体询问物业或网络管理员','0');
            }
            //检测手机号码是否被注册
            $_is_tel = Model('Home')->get(['tel'=>$data['tel']]);
            if($_is_tel) {
                return show('200','该手机号已被小区业主注册!','0');
            }
            //重置密码
            $data['password'] = lib\Password::setPassword($data['password']);
            //保存数据
            $_home_msg = Model('Home')->allowField(true)->save($data);
            if(!$_home_msg) {
                return show('200','数据库错误!','0');
            }
            return show('200','注册成功！欢迎'.$data['username'].' 您的加入!','1');
        }else {
            $_type  = Model('MemberType')->get_status_all_msg('1');   //楼栋
            $_index = Model('MemberIndex')->get_status_all_msg('1');   //单元门
            $_home  = Model('MemberHome')->get_status_all_msg('1');   //房间号
            $this->assign([
                '_type' => $_type,
                '_index' => $_index,
                '_home' => $_home,
            ]);
            return $this->fetch();
        }
    }

    public function home_index_edit()
    {
        if(\request()->isAjax()){
            $data = input('post.');
            //检测是否该用户已注册
            $_is_has = Model('Home')->where('id','neq',$data['id'])
                                          ->where(['type_name'=>$data['type_name'],'index_name'=>$data['index_name'],'home_name'=>$data['home_name']])
                                          ->select();
            if($_is_has) {
                return show('200','该业主已经被注册!','0');
            }
            //检测手机号码是否被注册
            $_is_tel = Model('Home')->where(['tel'=>$data['tel']])
                                          ->where('id','neq',$data['id'])
                                          ->select();
            if($_is_tel) {
                return show('200','该手机号已被小区业主注册!','0');
            }
            //保存数据
            $_home_msg = Model('Home')->save($data,['id'=>$data['id']]);
            if(!$_home_msg) {
                return show('200','数据库错误!','0');
            }
            return show('200','更新成功！','1');
        }else {
            $id = Request::instance()->param('id');
            $this_data = Model('Home')->get(['id'=>$id]);
            $this->assign([
                'this_data' => $this_data
            ]);
            $this->home_index_add();
            return $this->fetch();
        }
    }

    public function home_index_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('Home')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            return show('200','删除成功！','1');
        }else{
            return show('500','非法操作');
        }
    }
}